using System;
using System.IO.Ports; 

//documentatie: http://arduino.cc/playground/Interfacing/Mono

namespace POCarduinoconnection
{
	public class GGarduinoconnect
	{
		SerialPort connector;
		public event EventHandler trainComing;
		
		/*<summary>
		de constructor heeft een poortadres nodig en gebruikt dat adres om verbinding te maken met de arduino
		de standaard boudRate is 9600, niet veranderen A.U.B.
		</summary>
		*/
		
		
		public GGarduinoconnect (string comPoort)
		{
			connector = new SerialPort(comPoort, 9600);
			Console.WriteLine("POORT IS AANGEMAAKT");
			if (connector != null)
			{
				try
			{
				connector.Open();
				connector.WriteLine("#MAINROADRED%");
				connector.WriteLine("#SIDEROADGREEN%");
				connector.DataReceived += new SerialDataReceivedEventHandler(receiveData);
			}
			catch
			{
			Console.WriteLine("error connecting to arduino");
			connector.Close();	
			}
			}
			else
			{
			Console.WriteLine("connectie is null");
			}
			
			
		}
		
		
		public void mainRoadRed()
		{
			connector.WriteLine("#MAINROADRED%");
		}
		
		public void mainRoadYellow()
		{	
			connector.WriteLine("#MAINROADYELLOW%");
		}
		public void mainRoadGreen()
		{	
			connector.WriteLine("#MAINROADGREEN%");
		}
		public void sideRoadRed()
		{
			connector.WriteLine("#SIDEROADRED%");
		}
		
		public void sideRoadYellow()
		{	

			connector.WriteLine("#SIDEROADYELLOW%");
		}
		public void sideRoadGreen()
		{	
			connector.WriteLine("#SIDEROADGREEN%");
		}
		public void receiveData(object sender, EventArgs e)
			{
			string incoming = connector.ReadLine();
			Console.WriteLine(incoming);
				Console.WriteLine("train event triggered");
				trainComing(this, e);	
				}
		
		~GGarduinoconnect ()
		{
			//nette destructor om connectie af te sluiten
			connector.Close();
		}
	}
}

